<?php

namespace App\Http\Controllers;

use App\Availability;
use App\Order;
use App\Payment;
use App\Price;
use App\RoomType;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session;

class OrderController extends Controller
{
    public function index()
    {
        $customer = Auth::user();
        $orders = $customer->orders()->with(['availability', 'payment'])->paginate();
        return view('orderList', ['orders' => $orders]);
    }

    public function create()
    {
        $today = Carbon::today()->format('Y-m-d');
        return view('order', ['today' => $today]);
    }

    public function edit(Request $request)
    {
        $id = $request->get('id');
        $order = Order::findOrFail($id);
        $today = Carbon::today()->format('Y-m-d');
        return view('changeOrder', ['order' => $order], ['today' => $today]);
    }

    public function order(Request $request){
        // get 'from', 'to', 'roomType' from request
        $begin = $request->get('begin');
        $end = $request->get('end');
        $type_id = $request->get('type');
        $from = date_create($begin);
        $to = date_create($end);
        $len = date_diff($to, $from)->d;
        // get price of roomType
        $type = RoomType::with('price')->find($type_id);
        $price = $type->price->price * $len + 100;
        // write information to session
        $info = [
            'new' => true,
            'order' => null,
            'from' => $begin,
            'to' => $end,
            'type' => $type,
            'price' => $price,
        ];
        Session::put('pay_info', $info);
        // redirect to payment page
        return redirect()->route('showPayForm');
    }

    public function update(Request $request)
    {
        // check if it can be updated
        $order = Order::findOrFail($request->get('order'));
        if($order->cancelled || $order->check_in){
            return redirect()->route('home');
        }
        if($order->customer_id !== Auth::user()->id){
            return redirect()->route('home');
        }
        // get information from request and save it into session
        $begin = $request->get('end');
        $end = $request->get('end');
        $from = date_create($begin);
        $to = date_create($end);
        $len = date_diff($to, $from)->d;
        $type_id = $request->get('type');
        $type = RoomType::with('price')->find($type_id);
        $price = ($type->price->price * $len + 100) - $order->payment->paid;
        $info = [
            'new' => false,
            'order' => $order,
            'from' => $begin,
            'to' => $end,
            'type' => $type,
            'price' => $price
        ];
        Session::put('pay_info', $info);
        // redirect to payment page
        return redirect()->route('showPayForm');
    }

    public function store(Request $request)
    {
        $customer = Auth::user();
        $pay_info = Session::get('pay_info');
        if($pay_info['new']){
            //payment
            $payment = new Payment([
                'total' => $pay_info['price'],
                'paid' => $pay_info['price'],
            ]);
            // availability
            $availability = new Availability([
                'from' => $pay_info['from'],
                'to' => $pay_info['to'],
                'is_valid' => true,
            ]);
            $availability->room_type()->associate($pay_info['type']);
            // order
            $order = $customer->orders()->create([
                'check_in' => false,
                'cancelled' => false,
            ]);
            // save all
            $order->availability()->save($availability);
            $order->payment()->save($payment);
        } else {
            $order = $pay_info['order'];
            $availability = $order->availability;
            $payment = $order->payment;
            $availability->from = $pay_info['from'];
            $availability->to = $pay_info['to'];
            $availability->room_type()->associate($pay_info['type']);
            $payment->total += $pay_info['price'];
            $payment->paid += $pay_info['price'];
            $payment->save();
            $availability->save();
        }
//        return dd($order);
        // show success page
        return redirect()->route('orderSuccess');
    }

    public function success(){
        $pay_info = Session::get('pay_info');
        //forget pay_info in session
        Session::forget('pay_info');
        return view('orderSuccess', ['payinfo' => $pay_info]);
    }

    public function delete(Request $request) {
        // check if it can be deleted/cancelled
        $order = Order::findOrFail($request->get('id'));
        if($order->customer_id !== Auth::user()->id){
            return redirect()->route('home');
        }
        $avail = $order->availability;
        // if it's cancelled or check-in-ed
        // it should be deleted
        if($order->cancelled || $order->check_in){
            $avail->delete();
            $order->delete();
        } else {
            // else, it should be cancelled
            // make availability info invalid
            $avail->is_valid = false;
            // mark order info as cancelled
            $order->cancelled = true;
            // save them
            $avail->save();
            $order->save();
        }
        return redirect()->back();
    }
}
